from django.shortcuts import render

# Create your views here.
from django_redis import get_redis_connection
from rest_framework.generics import CreateAPIView
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView

from goods.models import SKU
from decimal import Decimal

from orders.serializers import OrderSettlementSerializer, CommitOrderSerializer


class OrderSettlementView(APIView):
    """去结算接口"""
    permission_classes = [IsAuthenticated]  # 指定权限,必须是登录用户才能访问此视图的接口

    def get(self, request):
        # 创建redis连接对象
        redis_conn = get_redis_connection('cart')
        # 获取user对象
        user = request.user
        # 获取redis中hash和set两个数据
        cart_dict_redis = redis_conn.hgetall('cart_%d' % user.id)

        selected_ids = redis_conn.smembers('selected_%d' % user.id)

        # 定义一个字典来保存勾选的商品及count
        cart_dict = {}  # {1:2,16:1}
        # 把hash中那些勾选商品的sku_id和count取出来包装到一个新字典中
        for sku_id_bytes in selected_ids:

            # bytes类型转为int类型
            cart_dict[int(sku_id_bytes)] = int(cart_dict_redis[sku_id_bytes])
            print(cart_dict)
        # 把勾选的商品的sku模型再获取出来
        # skus=SKU.objects.filter(id__in=selected_ids)
        skus = SKU.objects.filter(id__in=cart_dict.keys())
        # 遍历skus查询集取出一个一个的sku模型
        for sku in skus:
            # 给每一个sku模型多定义一个count属性 cart_dict是一个字典,cart_dict[sku.id]指cart_dict 的key对应的value,也就是sku的count
            sku.count = cart_dict[sku.id]

        # 定义-运费
        freight = Decimal('10.00')

        data_dict = {'freight': freight, 'skus': skus}  # 序列化时,可以对单个模型/查询集/列表/字典 都可以进行序列化器()

        # 创建序列化器进行序列化
        serializer = OrderSettlementSerializer(data_dict)

        return Response(serializer.data)


class CommitOrderView(CreateAPIView):
    """保存订单"""
    serializer_class = CommitOrderSerializer
    # 指定权限
    permission_classes = [IsAuthenticated]
